package com.baizhi.factory;

import java.io.InputStream;
import java.util.Properties;

import static java.lang.Class.forName;

public class BeanFactory {
    private static Properties env=new Properties();
    static {
        //读取配置文件
        InputStream in = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
        Object result = null;
        try {
            env.load(in);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static Object getBean(String id){
            Object result = null;
        try {
            String className = env.getProperty(id);
            //通过反射创建对象
            Class<?> c = forName(className);
             result = c.newInstance();

        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return result;
    }
}
